//
//  ProductTableViewCellMaker.m
//  nonghubao
//
//  Created by 王 文彬 on 13-3-10.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "ProductContentViewMaker.h"
#import "DynamicPicturesView.h"
#import "DynamicFooterView.h"
#import "Config.h"
#import "RTLabel.h"

#define PRODUCT_NAME_TEMPLATE @"名称：<font color='#86b60e'>%@</font>"
#define PRODUCT_PRICE_TEMPLATE @"价格：<font color='#86b60e'>%.2f元/%@</font>"

@implementation ProductContentViewMaker

+ (void)makePrototype:(UIView *)view
{
    //除商品名称和footer外，其余都没有真正的Y坐标，且高度都为0，且都为隐藏
    if (view) {
        //商品名Label
        RTLabel* productNameLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 5, 280, 20)];
        productNameLabel.font = _SYS_FONT_002;
        productNameLabel.backgroundColor = [UIColor clearColor];
        [view addSubview:productNameLabel];
        
        //商品价格
        RTLabel* priceLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        priceLabel.font = _SYS_FONT_002;
        priceLabel.backgroundColor = [UIColor clearColor];
        priceLabel.hidden = true;
        [view addSubview:priceLabel];
        
        //商品描述
        UITextView* summaryView = [[UITextView alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        summaryView.font = _SYS_FONT_002;
        summaryView.backgroundColor = [UIColor clearColor];
        summaryView.editable = false;
        summaryView.hidden = true;
        summaryView.scrollEnabled = false;
        [view addSubview:summaryView];
        
        summaryView.contentInset = UIEdgeInsetsMake(-5, -8, 0, 0);
        if (iOS7) {
            summaryView.contentInset = UIEdgeInsetsMake(-5, -5, 0, 0);
        }

        
        //商品产地
        UILabel* originLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        originLabel.font = _SYS_FONT_002;
        originLabel.backgroundColor = [UIColor clearColor];
        originLabel.numberOfLines = 0;
        originLabel.lineBreakMode = UILineBreakModeWordWrap;
        originLabel.hidden = true;
        [view addSubview:originLabel];
        
        //商品数量
        UILabel* amountLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        amountLabel.font = _SYS_FONT_002;
        amountLabel.backgroundColor = [UIColor clearColor];
        amountLabel.numberOfLines = 0;
        amountLabel.lineBreakMode = UILineBreakModeWordWrap;
        amountLabel.hidden = true;
        [view addSubview:amountLabel];
        
        //销售区域
        UILabel* regionLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 25, 280, 0)];
        regionLabel.font = _SYS_FONT_002;
        regionLabel.backgroundColor = [UIColor clearColor];
        regionLabel.numberOfLines = 0;
        regionLabel.lineBreakMode = UILineBreakModeWordWrap;
        regionLabel.hidden = true;
        [view addSubview:regionLabel];
        
        //商品图片
        UIView* picturesView = [[DynamicPicturesView alloc] initWithFrame:CGRectMake(10, 25, 290, 0)];
        picturesView.backgroundColor = [UIColor clearColor];
        picturesView.hidden = true;
        [view addSubview:picturesView];

        //统计
        RTLabel* footerLabel = [[RTLabel alloc] initWithFrame:CGRectMake(10, 25, 280, 20)];
        [view addSubview:footerLabel];
        
        //footer
//        UIView *footerView = [[DynamicFooterView alloc] initWithFrame:CGRectMake(5, 25, 280, 20) forStyle:FooterViewStyleMultiBig];
//        footerView.backgroundColor = [UIColor clearColor];
//        [view addSubview:footerView];
    }
}


// 计算产品动态的高度
// 高度为：商品名称 + 销售区域 + 商品描述 + 图片 + footer
+ (float)heightWithDictionary:(NSDictionary *)data
{
    //商品名称一定会有，如果商品是已删除，那么名字那里就会显示已删除
    float height = 20.0f;
    
    //取出商品对象
    NSDictionary* product = data[@"product"];
    
    //商品
    if (![product isKindOfClass:[NSNull class]] && [product[@"status"] intValue] == 0) {
        //价格 + 数量 + 产地
        height += 20 + 20 + 20;
        
        //获得销售区域的值
        NSString* region = [NSString stringWithFormat:@"销售区域：%@", product[@"salesRegion"]];
        CGSize regionSize = [region sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(290.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += regionSize.height;
   
        //获得商品描述的值
        NSString* description = [NSString stringWithFormat:@"产品描述：%@", product[@"description"]];
        CGSize descriptionSize = [description sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(260.0f, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
        height += descriptionSize.height + 10;
    
        //获得图片数组，并判断是否存在图片
        NSArray *pictures = product[@"pictures"];
    
        //有图片，且图片对象不为Null，且图片数量>0
        if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
            height += _PICTURE_NORMAL_PREVIEW_HEIGHT + 5 + 13;
        }
    }
    
    //footer高度
    height += 18;
    
    //高度还要加上各种间隙
    height += 5 + 5 + 5 + 5;

    return height;
}


// 根据数据重新绘制动态内容
+ (void)drawContentView:(UIView *)view withDictionary:(NSDictionary *)data
{

    //Y坐标的偏移，每多一个元素，就会偏移，用于定位全部元素的Y起点。从25开始，也就是从商品名称之后开始。25=5间隙+20Label高度。
    float offsetY = 25.0f;

    //取出商品对象
    NSDictionary* product = data[@"product"];

    //商品名称
    RTLabel* productNameLabel = view.subviews[0];
    if ([product isKindOfClass:[NSNull class]] || [product[@"status"] intValue] != 0) {
        productNameLabel.text = @"该商品已停售";
        
        //重置对象：第一个（商品名称）和最后一个（footer）不隐藏
        for (int i = 1; i < view.subviews.count - 1; i++) {
            ((UIView *)view.subviews[i]).hidden = true;
        }
    }
    else {
        productNameLabel.text = [NSString stringWithFormat:PRODUCT_NAME_TEMPLATE, product[@"name"]];

        //价格
        RTLabel* priceLabel = view.subviews[1];
        priceLabel.text = [NSString stringWithFormat:PRODUCT_PRICE_TEMPLATE, [product[@"price"] floatValue], product[@"unitName"]];
        
        //描述
        UITextView* summaryView = view.subviews[2];
        summaryView.text = [NSString stringWithFormat:@"描述：%@", product[@"description"]];

        //商品产地
        NSString* originPlace = product[@"provenance"] == nil ? @"" : product[@"provenance"];
        UILabel* originLabel = view.subviews[3];
        originLabel.text = [NSString stringWithFormat:@"产地：%@", originPlace];

        //库存
        UILabel* amountLabel = view.subviews[4];
        amountLabel.text = [NSString stringWithFormat:@"库存数量：%i", [product[@"inventory"] integerValue]];

        //销售区域
        UILabel* regionLabel = view.subviews[5];
        regionLabel.text = [NSString stringWithFormat:@"销售区域：%@", product[@"salesRegion"]];
        
        //TO-DO》》》》》》》》》此处需要优化《《《《《《《《
        
        //将价格、销售区域、描述显示出来，并重新计算他们的高度，累加offsetY
        RTLabel* label = view.subviews[1];
        label.hidden = false;
        
        //计算size
//        CGSize size = [label.text sizeWithFont:_SYS_FONT_YAHEI_12 constrainedToSize:CGSizeMake(280, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
//        float labelHeight = size.height < 20 ? 20 : size.height;
        
        //重置frame，累加offsetY
//        NSLog(@"%f", offsetY);
        label.frame = CGRectMake(priceLabel.frame.origin.x, offsetY, priceLabel.frame.size.width, 20);
//        offsetY += label.frame.size.height - 5.0f;
//        NSLog(@"-----%f", label.frame.size.height);
        offsetY = offsetY + label.frame.size.height - 5.0f;
        for (int i = 2; i < 6; i++) {
            
            //遍历出Label
            UILabel* label = view.subviews[i];
            label.hidden = false;
            
            //计算size
            CGSize size = [label.text sizeWithFont:_SYS_FONT_002 constrainedToSize:CGSizeMake(260, 200.0f) lineBreakMode:UILineBreakModeWordWrap];
            float labelHeight = size.height < 20 ? 20 : size.height;
            
            //重置frame，累加offsetY
//            NSLog(@"%f", offsetY);
            label.frame = CGRectMake(priceLabel.frame.origin.x, offsetY, priceLabel.frame.size.width, labelHeight + 15);
            offsetY += labelHeight;
        }
        
        //图片
        offsetY += 13;  //图片间距多13
        DynamicPicturesView* picturesView = view.subviews[6];
        
        NSArray* pictures = product[@"pictures"];
        if (pictures != nil && ![pictures isKindOfClass:[NSNull class]] && pictures.count > 0) {
            //重画图片
            picturesView.hidden = false;
            picturesView.frame = CGRectMake(picturesView.frame.origin.x, offsetY, picturesView.frame.size.width, _PICTURE_NORMAL_PREVIEW_HEIGHT);
            [picturesView drawPictures:pictures];
            
            offsetY += _PICTURE_NORMAL_PREVIEW_HEIGHT ;
        }
        else {
            [picturesView drawPictures:nil];
        }
    }

    //footer
    //设定footer的计数值。要注意，如果product是NSNull，那么trasactionCount和recommendcount需要判断，并处理为0
    int commented = [data[@"type"] integerValue] == DynamicCategoryRecommend ? [data[@"pdCommentCount"] integerValue] : [data[@"commentCount"] integerValue];
    int sold = [product isKindOfClass:[NSNull class]] ? 0 : [product[@"transactionCount"] integerValue];
    int recommended = [product isKindOfClass:[NSNull class]] ? 0 : [product[@"recommendCount"] integerValue];
    
    RTLabel* footerLabel = view.subviews[7];
    footerLabel.frame = CGRectMake(footerLabel.frame.origin.x, offsetY + 10, footerLabel.frame.size.width, footerLabel.frame.size.height);
    footerLabel.text = [NSString stringWithFormat:_L(@"tpl_33"), commented, recommended, sold];
    footerLabel.font = _SYS_FONT_002;
    
//    DynamicFooterView* footerView = view.subviews[7];
//    footerView.frame = CGRectMake(footerView.frame.origin.x, offsetY + 5, footerView.frame.size.width, footerView.frame.size.height);
//    [footerView setNumbersWithCommented:commented andSold:sold andRecommended:recommended];
    
    
    //重绘自己视图的|高度|
    view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width, footerLabel.frame.origin.y + footerLabel.frame.size.height);
}

@end
